﻿namespace Trading.IBApi

open System

open com.ib.client 

open Trading.Observable

module ExitStrategy =

    let stopLoss (position : Position) (priceTicks : Contract -> IObservable<double>) (lossLimitPct : double) =
        let isLimitReached (price : double) =
            let currentValue = price * double position.Size
            let deltaPct = (1. - currentValue / position.Cost) * 100.
            deltaPct >= lossLimitPct

        observable {
            let! price = priceTicks position.Contract
            if isLimitReached price then
                yield position
        }
